/***********************************************************************************************************************
 * 
 * > QuadroCore <
 * 
 * Copyright (C) 2012 by Chris Channing
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************/

#include "quadrocore.h"

void LCDWriteChar(uint8_t x, uint8_t y, char character, Font_t* fontP, FONT_COLOUR fontColour)
{
	if (! fontP)
	{
		fontP = GetDefaultFont();	
	}

	{
		uint8_t *glyphP = fontP->getCharacterGlyphFunc(fontP, character);
		uint8_t xx = x;
		uint8_t yy = y + fontP->height;
	
		if (! glyphP) return;
	
		for (uint8_t *glyphRowP = glyphP; glyphRowP < (glyphP + fontP->height); glyphRowP++)
		{
			for (uint8_t position = 8; position >= (8 - fontP->width); position--)
			{
				uint8_t pixel = (*glyphRowP  & (1 << position - 1)) != 0;
			
				if (pixel)
				{
					if (BLACK == fontColour)
					{
						LCDCanvasDrawPixel(xx, yy);	
					}
					else
					{
						LCDCanvasClearPixel(xx, yy);
					}
				}	
					
				xx++;
			}
		
			yy--;
			xx = x;
		}
	}		
}

void LCDWriteString(uint8_t x, uint8_t y, char *stringP, Font_t* fontP, FONT_COLOUR fontColour)
{
	if (! stringP) return;
	
	if (! fontP)
	{
		fontP = GetDefaultFont();
	}
	
	{
		uint8_t xx = x;
		
		for (char *charP = stringP; *charP; charP++)
		{
			LCDWriteChar(xx, y, *charP, fontP, fontColour);
			xx += fontP->width + 1;
		}
	}		
}